﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using LMHTX.Modules.DIRECTORY.COOPERATIVE.Components;
using System.Web;
using Type = LMHTX.Modules.DIRECTORY.COOPERATIVE.Components.Type;

namespace LMHTX.Modules.DIRECTORY.COOPERATIVE.Controls
{
    public partial class CooperativeDetail : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                for (int i = 1975; i <= DateTime.Today.Year; i++)
                {
                    ddlHistoryYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                ddlHistoryYear.SelectedValue = (DateTime.Today.Year - 1).ToString();

                if (this.ItemID != 0)
                {
                    Cooperative cooperative = Cooperative.Load(this.ItemID);
                    this.LoadHistoryData(DateTime.Today.Year);

                    Career career = Career.Load(cooperative.CareerID);
                    Area area = Area.Load(cooperative.AreaID);
                    Type type = Type.Load(cooperative.TypeID);

                    lblTitle.Text = cooperative.Title;
                    lblCareer.Text = career.Title;
                    lblArea.Text = area.Title;
                    lblType.Text = type.Title;

                    Employee chairman = Employee.Load(cooperative.ChairmanID);
                    btnChairman.Text = chairman.FullName;
                    lblChairmanFullName.Text = chairman.FullName;
                    lblChairmanTrainingType.Text = TrainingType.Load(chairman.TrainingTypeID).Title;
                    lblChairmanTrainingLevel.Text = TrainingLevel.Load(chairman.TrainingLevelID).Title;
                    lblChairmanAppointingYear.Text = chairman.AppointingYear.ToString();


                    if (cooperative.ViceChairmanID > 0)
                    {
                        Employee vicechairman = Employee.Load(cooperative.ViceChairmanID);
                        btnViceChairman.Text = vicechairman.FullName;
                        lblViceChairmanFullName.Text = vicechairman.FullName;
                        lblViceChairmanTrainingType.Text = TrainingType.Load(vicechairman.TrainingTypeID).Title;
                        lblViceChairmanTrainingLevel.Text = TrainingLevel.Load(vicechairman.TrainingLevelID).Title;
                        lblViceChairmanAppointingYear.Text = vicechairman.AppointingYear.ToString();
                    }

                    Employee chiefAccountant = Employee.Load(cooperative.ChiefAccountantID);
                    btnChiefAccountant.Text = chiefAccountant.FullName;
                    lblChiefAccountantFullName.Text = chiefAccountant.FullName;
                    lblChiefAccountantTrainingType.Text = TrainingType.Load(chiefAccountant.TrainingTypeID).Title;
                    lblChiefAccountantTrainingLevel.Text = TrainingLevel.Load(chiefAccountant.TrainingLevelID).Title;
                    lblChiefAccountantAppointingYear.Text = chiefAccountant.AppointingYear.ToString();


                    if (cooperative.ControllerID > 0)
                    {
                        Employee controller = Employee.Load(cooperative.ControllerID);
                        btnController.Text = controller.FullName;
                        lblControllerFullName.Text = controller.FullName;
                        lblControllerTrainingType.Text = TrainingType.Load(controller.TrainingTypeID).Title;
                        lblControllerTrainingLevel.Text = TrainingLevel.Load(controller.TrainingLevelID).Title;
                        lblControllerAppointingYear.Text = controller.AppointingYear.ToString();
                    }

                    lblEmail.Text = cooperative.Email;
                    lblWebsite.Text = cooperative.Website;
                    lblAddress.Text = cooperative.Address;
                    lblPhone.Text = cooperative.Phone;
                    lblFax.Text = cooperative.Fax;
                    lblEstablishedYear.Text = cooperative.EstablishedYear.ToString();
                    lblLicenseNumber.Text = cooperative.LicenseNumber;

                    // Thị trường
                    lblMarketDescription.Text = cooperative.MarketDescription;
                    chkHasInlandMarket.Checked = cooperative.HasInlandMarket;
                    chkHasOutlandMarket.Checked = cooperative.HasOutlandMarket;
                    // Tổ chức / Đoàn thể
                    lblOrganizationDescription.Text = cooperative.OrganizationDescription;

                    chkHasPartyOrganization.Checked = cooperative.HasPartyOrganization;
                    chkHasTradeUnion.Checked = cooperative.HasTradeUnion;
                    chkHasTheCommunistYouthLeague.Checked = cooperative.HasTheCommunistYouthLeague;
                    chkHasWomanUnion.Checked = cooperative.HasWomanUnion;

                    lblProduct.Text = HttpUtility.HtmlDecode(cooperative.ProductDescription);
                    lblDescription.Text = cooperative.Description;
                }
            }
        }

        protected void ddlHistoryYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            int year = Convert.ToInt32(ddlHistoryYear.SelectedValue);
            this.LoadHistoryData(year);
        }

        private void LoadHistoryData(int year)
        {
            History history = History.SelectCollectionBy_CooperativeID(this.ItemID).FirstOrDefault(x => x.Year == year);
            if (history != null)
            {
                lblCharterCapital.Text = history.CharterCapital.ToString("N0");
                lblWorkingCapital.Text = history.WorkingCapital.ToString("N0");
                lblFixedCapital.Text = history.FixedCapital.ToString("N0");
                lblTurnover.Text = history.Turnover.ToString("N0");
                lblTax.Text = history.Tax.ToString("N0");
                lblProfit.Text = history.Profit.ToString("N0");

                lblLabourQuantity.Text = history.LabourQuantity.ToString("N0");
                lblWomanLabourQuantity.Text = history.WomanLabourQuantity.ToString("N0");
                lblCooperatorQuantity.Text = history.CooperatorQuantity.ToString("N0");
                lblWomanCooperatorQuantity.Text = history.WomanCooperatorQuantity.ToString("N0");
                lblRank.Text = history.Rank;

                // Tổ chức / Đoàn thể
                lblPartyMemberQuantity.Text = history.PartyMemberQuantity.ToString("N0");
            }
            else
            {
                const string empty = "[Chưa có số liệu]";
                lblCharterCapital.Text = empty;
                lblWorkingCapital.Text = empty;
                lblFixedCapital.Text = empty;
                lblTurnover.Text = empty;
                lblTax.Text = empty;
                lblProfit.Text = empty;

                lblLabourQuantity.Text = empty;
                lblWomanLabourQuantity.Text = empty;
                lblCooperatorQuantity.Text = empty;
                lblWomanCooperatorQuantity.Text = empty;
                lblRank.Text = empty;

                // Tổ chức / Đoàn thể
                lblPartyMemberQuantity.Text = empty;
            }
        }
    }
}